package com.miku.project.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.mikuapicommon.model.entity.ProductInfo;
import com.miku.project.common.ErrorCode;
import com.miku.project.exception.BusinessException;
import com.miku.project.mapper.ProductInfoMapper;
import com.miku.project.service.ProductInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author ThinkBook
* @description 针对表【product_info(产品信息)】的数据库操作Service实现
* @createDate 2024-06-20 18:57:11
*/
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo>
    implements ProductInfoService {

    /**
     * 校验有效信息信息
     *
     * @param productInfo
     * @param add
     */
    @Override
    public void validProductInfo(ProductInfo productInfo, boolean add) {
        if (productInfo == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品信息不存在");
        }
        String name = productInfo.getName();
        String description = productInfo.getDescription();
        Long total = productInfo.getTotal();
        Long addPoints = productInfo.getAddPoints();

        //创建时,所有参数必须非空
        if (add){
            if (StringUtils.isAnyBlank(name)){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"产品名称不能为空");
            }
        }
        if (addPoints<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"积分不能为负数");
        }
        if (total<0 || total>10000000000L){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"库存必须大于0且小于10000000000");
        }
        if (description!=null && description.length()>100){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"描述过长");
        }


    }
}




